﻿Imports MICROFORMA.BEL
Imports MICROFORMA.DIL
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Common

Public Class DocumentoDA
    Implements DIL.DocumentosDI

    Public Function ListAll(ByVal IdLote As Integer) As System.Data.DataTable Implements DIL.DocumentosDI.ListAll

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String = "select * from Documentos where IdLote=" & IdLote & " Order By 1" 'TODO: HardCode

        Dim dbCommand As DbCommand = db.GetSqlStringCommand(query)
        Dim EscanerDataSet As DataSet = Nothing
        EscanerDataSet = db.ExecuteDataSet(dbCommand)
        Return EscanerDataSet.Tables(0)
    End Function
    Public Function UltimoDocumento() As Integer

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Return db.ExecuteScalar(CommandType.Text, "SELECT IDENT_CURRENT('Documentos')")

    End Function
    Public Function AgregaMetada(ByVal StrString As String) As String

        Dim db As Database = DatabaseFactory.CreateDatabase()

        Try

            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(StrString)
            db.ExecuteNonQuery(o_dbCommand)

            Return "OK"

        Catch ex As Exception

            Return ex.Message.ToString

        End Try


    End Function
    Public Function Add(ByVal documentoBE As DocumentosBE) As String Implements DIL.DocumentosDI.Add

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim query As String
        Try

            query = "Insert into Documentos(IdLote,IdTipoDocumental,Titulo,RutaArchivo,Paginas) values(" _
                & documentoBE.IdLote & "," & documentoBE.IdTipoDocumental & ",'" & documentoBE.Titulo _
                & "','" & documentoBE.Archivo & "'," & documentoBE.Paginas & ")"

            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)
            db.ExecuteNonQuery(o_dbCommand)

            Return "OK"

        Catch ex As Exception

            Return ex.Message.ToString

        End Try

    End Function

End Class

